<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Statement" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>数据库查询</title>
</head>
<body>
<%
try {
	//1、注册MySQL驱动
	Class.forName("com.mysql.jdbc.Driver");
	//2、获取数据库连接
	Connection con = DriverManager.getConnection(
			"jdbc:mysql://127.0.0.1:3306/mydatabase?useUnicode=true&characterEncoding=utf8", 
			"root", 
			"root123");
	//3、创建Statement对象
	Statement st = con.createStatement();  
	//4、执行SQL
	ResultSet rs = st.executeQuery("select * from user");
	//5、打印结果
	while (rs.next()) {
		out.print(
				rs.getString("name") + " " 
				+ rs.getString("password") + "<br/>");
	}
	//6、关闭连接
	rs.close();
	st.close();
	con.close();
} catch (Throwable e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
%>
</body>
</html>